package club.hicode.daydayup.csv;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.StringReader;
import java.util.List;
import java.util.Map;

/**
 * 测试 CSV
 *
 * @author Liu Chunfu
 * @date 2018-05-14 下午2:35
 **/
public class CSVTest {
    @Test
    public void parseTest1() {
        StringReader reader = StrUtil.getReader("aaa,b\"bba\",ccc");
        CsvParser parser = new CsvParser(reader, null);
        CsvRow row = parser.nextRow();
        Map<String, String> fieldMap = row.getFieldMap();
        fieldMap.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
        Assert.assertEquals("b\"bba\"", row.getRawList().get(1));
        IoUtil.close(parser);
    }

    @Test
    public void testCSV2() {
        CsvReader reader = new CsvReader();
        CsvData data = reader.read(new File("/Users/liuchunfu/Develop/temp/data.csv"));
        List<CsvRow> rows = data.getRows();
        for (CsvRow row : rows) {
            //Map<String, String> fieldMap = row.getFieldMap();
            //fieldMap.forEach((k, v) -> System.out.println(k + ":" + v));

            System.out.println(row.get(1));
        }

    }
}
